import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../color.dart';
import '../../../log_util.dart';
import '../../remove_blue_scroll_behavior.dart';
import 'pager_mixin.dart';

/// @author: pengboboer
/// @createDate: 2022/1/11
class PagerRefreshUtils {
  static Widget bind(
      {required PagerMixin viewModel,
      Widget? refreshView,
      Function? onRefresh,
      Function? onLoadMore,
      ScrollBehavior? scrollBehavior,
      Widget? emptyWidget,
      EdgeInsets? footerPadding,
      bool isNeedShowLoading = true, // 是否需要显示loading状态
      bool isResetRefreshLoading = false, // 重新refresh时是否需要重新显示页面中间的loading
      bool enablePullUp = true,
      bool enablePullDown = false}) {
    return loadingProgressWithColor(
      isNeedShowLoading ? viewModel.isShowLoading : false,
      child: ScrollConfiguration(
        behavior: scrollBehavior ?? RemoveBlueScrollBehavior(),
        child: RefreshConfiguration(
          hideFooterWhenNotFull: true,
          child: elRefresh(
              refreshController: viewModel.refreshController,
              enablePullDown: enablePullDown,
              enablePullUp: enablePullUp,
              onRefresh: () {
                if (onRefresh != null) {
                  onRefresh();
                } else {
                  viewModel.onRefresh(isResetLoading: isResetRefreshLoading);
                }
              },
              onLoading: onLoadMore ?? viewModel.onLoadMore,
              footerPadding: footerPadding,
              pageNum: viewModel.currentPage,
              child: viewModel.isShowEmpty
                  ? (emptyWidget ?? buildCommonEmptyWidget())
                  : refreshView),
        ),
      ),
    );
  }

  static Widget buildCommonEmptyWidget(
      {Text? text, Image? image, double? margin, Color? color}) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: color ?? ColorManager.colorFFFFFF,
      padding: EdgeInsets.only(top: 160.w, left: 75.w, right: 75.w),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          image ??
              Image.asset(
                "images/default_empty.webp",
                width: 90.w,
                height: 90.w,
                package: "hopson_utils",
              ),
          SizedBox(height: margin ?? 12.w),
          text ??
              Text(
                "暂无相关数据",
                style: TextStyle(color: Color(0xff999999), fontSize: 14.sp),
              ),
        ],
      ),
    );
  }

  ///刷新控件封装
  static Widget elRefresh(
      {refreshController,
      child,
      onRefresh,
      onLoading,
      noDataText, //自定义加载完数据显示的文本
      pageNum, //当前加载页数
      footerPadding,
      enablePullUp = true,
      enablePullDown = true}) {
    return SmartRefresher(
      controller: refreshController,
      enablePullUp: enablePullUp,
      enablePullDown: enablePullDown,
      header: Platform.isAndroid
          ? MaterialClassicHeader(
              color: ColorManager.colorF65247,
            )
          : CustomHeader(
              height: 35.w,
              builder: (BuildContext context, RefreshStatus? mode) {
                Widget header;
                LogUtils.d(mode);
                if (mode == RefreshStatus.idle) {
                  header = Text("松手,加载!");
                } else if (mode == RefreshStatus.refreshing) {
                  header = CupertinoActivityIndicator();
                } else if (mode == RefreshStatus.failed) {
                  header = Text("加载失败！点击重试！");
                } else if (mode == RefreshStatus.canRefresh) {
                  header = Text("松手,加载!");
                } else {
                  header = Text("加载完成");
                }
                return Container(
                  alignment: Alignment.center,
                  child: header,
                );
              },
            ),
      footer: CustomFooter(
        height: 50.w,
        builder: (BuildContext context, LoadStatus? mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = Text("上拉加载");
          } else if (mode == LoadStatus.loading) {
            body = CupertinoActivityIndicator();
          } else if (mode == LoadStatus.failed) {
            body = Text("加载失败！点击重试！");
          } else if (mode == LoadStatus.canLoading) {
            body = Text("松手,加载更多!");
          } else {
            body = Text(pageNum != 1 ? noDataText ?? '没有更多数据了' : "");
          }
          return Container(
            height: 55.0,
            padding: footerPadding,
            alignment: Alignment.center,
            child: body,
          );
        },
      ),
      child: child ?? Container(),
      onRefresh: onRefresh,
      onLoading: onLoading,
    );
  }
}

///loading 布局
Widget loadingProgressWithColor(bool isLoading,
    {Widget? child, Color? bgColor}) {
  return Stack(
    children: [
      child!,
      Offstage(
        offstage: !isLoading,
        child: Container(
          alignment: Alignment.center,
          width: double.infinity,
          height: double.infinity,
          color: bgColor ?? Colors.white,
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation<Color>(ColorManager.colorF87E75),
          ),
        ),
      )
    ],
  );
}
